package com.chuangyang.fixboxmaster.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CostItem;

import java.util.ArrayList;


/**
 * Created by Administrator on 2015/6/3.
 */
public class CostDetailAdapter extends BaseAdapter{

    private Context mContext;

    private ArrayList<CostItem> mCostItems;

    public CostDetailAdapter(Context context, ArrayList<CostItem> costItems){
        mContext = context;

        mCostItems = costItems;

    }
    @Override
    public int getCount() {
        return mCostItems == null?0:mCostItems.size();
    }

    @Override
    public CostItem getItem(int position) {
        return mCostItems == null?null:mCostItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.cost_detail_item, parent, false);
            holder.mTitle = (TextView)convertView.findViewById(R.id.cost_detail_item_title);
            holder.mNum = (TextView)convertView.findViewById(R.id.cost_detail_item_num);
            holder.mTotal = (TextView)convertView.findViewById(R.id.cost_detail_item_total_price);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        CostItem item = getItem(position);

        if (item != null)
        {
            holder.mTitle.setText(item.title);
            if (item.type == 3)
            {
                holder.mNum.setVisibility(View.VISIBLE);
                holder.mNum.setText("×"+item.num);
            }
            else if (item.type == 1 || item.type == 2)
            {
                holder.mNum.setVisibility(View.INVISIBLE);
            }
            holder.mTotal.setText("￥"+(item.price*item.num));
        }
        return convertView;
    }


    public class ViewHolder{

        public TextView mTitle;

        public TextView mNum;

        public TextView mTotal;

    }
}
